﻿@{
    Layout = null;
}

<div>
    <div class="col-3 pr-0" style="display: none">
        <label id="AddRetentionUse-RetentionSampleIds">@ViewData["RetentionSampleIds"]</label>
        <label id="AddRetentionUse-Parent-ModalID">@ViewData["ModalID"]</label>
    </div>
    <div class="input-group">
        <div class="col-3 pr-0">
            <span class="input-group-text font-rfs border-radius-noright noRightBorder">包装数量*</span>
        </div>
        <div class="col-9 pl-0 input-group position-relative">
            <input id="AddRetentionUse-ContainerNumber" class="form-control font-rfs border-radius-noleft" style="width:45%" value="0"/>
        </div>
    </div>
    <div class="input-group">
        <div class="col-3 pr-0">
            <span class="input-group-text font-rfs border-radius-noright noRightBorder">领用总量*</span>
        </div>
        <div class="col-9 pl-0 input-group position-relative">
            <input id="AddRetentionUse-Quantity" class="form-control font-rfs border-radius-noleft" placeholder="必需是有效数字" />
        </div>
    </div>
    <div class="input-group">
        <div class="col-3 pr-0">
            <span class="input-group-text font-rfs border-radius-noright noRightBorder">备注</span>
        </div>
        <div class="col-9 pl-0">
            <input id="AddRetentionUse-Note" class="form-control font-rfs border-radius-noleft" placeholder="可选" />
        </div>
    </div>
    <div id="workflow" style="display: none">
        <div class="input-group" style="padding-right: 15px;">
            <div class="col-12 pr-0">
                <span class="input-group-text font-rfs border-radius-noright" style="justify-content:center">请选择审批流程和审批人</span>
            </div>
        </div>
        <div class="input-group">
            <div class="col-3 pr-0">
                <span class="input-group-text font-rfs border-radius-noright noRightBorder">审批流程*</span>
            </div>
            <div class="col-9 pl-0 input-group">
                <select id="AddRetentionUse-WorkFlow" class="form-control font-rfs border-radius-noleft" style="width:90%"></select>
            </div>
        </div>
        <div class="input-group">
            <div class="col-3 pr-0">
                <span class="input-group-text font-rfs border-radius-noright noRightBorder">审批人*</span>
            </div>
            <div class="col-9 pl-0 input-group">
                <select id="AddRetentionUse-User" class="form-control font-rfs border-radius-noleft" style="width:90%"></select>
            </div>
        </div>
        <div class="input-group">
            <div class="col-3 pr-0">
                <span class="input-group-text font-rfs border-radius-noright noRightBorder">发起说明</span>
            </div>
            <div class="col-9 pl-0">
                <input id="AddRetentionUse-Description" class="form-control font-rfs border-radius-noleft" placeholder="可选" />
            </div>
        </div>
    </div>
    <div class="text-right">
        <button id="AddRetentionUse-confirm" class="btn btn-primary font-rfs">确定</button>
        <button id="AddRetentionUse-cancel" type="button" class="btn btn-info font-rfs">取消</button>
    </div>
</div>

<script>

    $(document).ready(function () {
        $.post("GetRetentionSampleById", {
            RetentionSampleId: $("#AddRetentionUse-RetentionSampleIds").html().split(',')[0]
        }, function (data) {
            if (data.useApproval == true) {
                $("#workflow").show();
                LoadWorkFlowByType('AddRetentionUse-WorkFlow', "审核");
                LoadUsers('AddRetentionUse-User');
            }
        });
    });

    $("#AddRetentionUse-confirm").click(function () {
        if ($.trim($("#AddRetentionUse-ContainerNumber").val()).length == 0) {
            alert("包装数量不能为空！");
            $("#AddRetentionUse-ContainerNumber").addClass('error-style');
            return false;
        }
        if ($.trim($("#AddRetentionUse-Quantity").val()).length == 0) {
            alert("领用数量不能为空！");
            $("#AddRetentionUse-Quantity").addClass('error-style');
            return false;
        }

        if (!$("#workflow").is(':hidden')) {
            AddInventoryWF();
        } else {
            $.post("postAddRetentionUse", {
                RetentionSampleIds: $("#AddRetentionUse-RetentionSampleIds").html(),
                ContainerNumber: $.trim($("#AddRetentionUse-ContainerNumber").val()),
                Quantity: $.trim($("#AddRetentionUse-Quantity").val()),
                Note: $.trim($("#AddRetentionUse-Note").val())
            }, function (data) {
                alert(data);
                $("#AddRetentionUse-cancel").click();
            });
        }
    });

    function AddInventoryWF() {
        if ($("#AddRetentionUse-WorkFlow").val().length == 0) {
            alert("必需选择要发起的流程！");
            $("#AddRetentionUse-WorkFlow").addClass('error-style');
            return false;
        }
        if ($("#AddRetentionUse-User").val().length == 0) {
            alert("必需选择一个审批人！");
            $("#AddRetentionUse-User").addClass('error-style');
            return false;
        }

        var StarWFParameters = {};
        StarWFParameters["WorkFlowId"] = $("#AddRetentionUse-WorkFlow").find("option:selected").val();
        StarWFParameters["DomainName"] = "RetentionUse";
        StarWFParameters["DomainId"] = ''; /* 这个 DomainId 是在后台获得的 */
        StarWFParameters["ApprovedByID"] = $("#AddRetentionUse-User").find("option:selected").val();
        StarWFParameters["ApprovedByName"] = $("#AddRetentionUse-User").find("option:selected").text();

        $.post("postAddRetentionUse", {
            RetentionSampleIds: $("#AddRetentionUse-RetentionSampleIds").html(),
            ContainerNumber: $.trim($("#AddRetentionUse-ContainerNumber").val()),
            Quantity: $.trim($("#AddRetentionUse-Quantity").val()),
            Note: $.trim($("#AddRetentionUse-Note").val()),
            StarWFParameters: JSON.stringify(StarWFParameters)
        }, function (data) {
            if (data.indexOf("成功") >= 0) {
                $("#AddRetentionUse-cancel").click();
            } else {
                alert(data);
            }
        });
    }

    $("#AddRetentionUse-cancel").click(function () {
        var modalID = $("#AddRetentionUse-Parent-ModalID").html();
        $("#" + modalID).modal('hide');
    });

</script>
